package event;

/**
 *  事件驱动风格
 */

public class EventDriven {
    public String process() {
        EventDispatcher dispatcher = new EventDispatcher();
        return dispatcher.handleEvent();
    }

    class EventDispatcher {
        private String[] lines = {
            "Liu Jin Qi is learning",
            "Software architecture is essential",
            "Understanding design patterns is crucial"
        };

        public String handleEvent() {
            StringBuilder result = new StringBuilder();
            for (String line : lines) {
                result.append(triggerEvent(line)).append("\n");
            }
            return result.toString();
        }

        private String triggerEvent(String line) {
            return kwicShift(line);
        }

        private String kwicShift(String line) {
            String[] words = line.split(" ");
            StringBuilder shifted = new StringBuilder();
            for (int i = 0; i < words.length; i++) {
                for (int j = i; j < words.length; j++) {
                    shifted.append(words[j]).append(" ");
                }
                for (int k = 0; k < i; k++) {
                    shifted.append(words[k]).append(" ");
                }
                shifted.append("\n");
            }
            return shifted.toString();
        }
    }
}
